package com.whoimi.admin.rest;

import com.whoimi.admin.entity.form.MenuForm;
import com.whoimi.admin.entity.vo.MenuVO;
import com.whoimi.admin.entity.vo.RouteVO;
import com.whoimi.admin.model.Option;
import com.whoimi.admin.service.MenuService;
import com.whoimi.common.ResultInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author whoimi
 * @since 2025-07-25
 **/
@RestController
@RequestMapping("/api/v1/menus")
public class MenuRest {
    @Resource
    private MenuService menuService;

    /**
     * 菜单列表
     */
    @GetMapping
    public ResultInfo<List<MenuVO>> listMenus() {
        List<MenuVO> menuList = menuService.listMenus();
        return ResultInfo.ok(menuList);
    }

    @GetMapping("/routes")
    public ResultInfo<List<RouteVO>> getCurrentUserRoutes() {
        List<RouteVO> routeList = menuService.getCurrentUserRoutes("admin");
        return ResultInfo.ok(routeList);
    }

    @PutMapping(value = "/{id}")
    public ResultInfo<Void> updateMenu(@RequestBody MenuForm menuForm) {
        menuService.saveMenu(menuForm);
        return ResultInfo.ok();
    }

    @GetMapping("/options")
    public ResultInfo<List<Option<Long>>> listMenuOptions(@RequestParam(required = false, defaultValue = "false") boolean onlyParent) {
        List<Option<Long>> menus = menuService.listMenuOptions(onlyParent);
        return ResultInfo.ok(menus);
    }

    @GetMapping("/{id}/form")
    public ResultInfo<MenuForm> getMenuForm(@PathVariable Long id) {
        MenuForm menu = menuService.getMenuForm(id);
        return ResultInfo.ok(menu);
    }
}
